<?php				
	interface io_class_info
	{		
		function getIOList($CostCode,$FromDate,$ToDate,$ReceiptPerson,$DeliveryPerson,$Remark);	
		function getInvcList($InvoiceID,$RecvNoteCode,$FromDate,$ToDate,$ReceiptPerson,$DeliveryPerson,$RecvRoom,$Note);
		
		//Thêm phiếu nhập xuất vào database table recv_note_tbl
		function insertIORow($invcID,$IOCode,$ReceiptDate,$revPerson,$devPerson,$note,$modifiedBy,$DiscountVND,$SubTotalVND,$VATAmountVND,$TotalVND,$DisPercent,$VATPercent);
		function updateIORow($revNoteID,$invcID,$ReceiptDate,$revPerson,$devPerson,$note,$modifiedBy,$DiscountVND,$SubTotalVND,$VATAmountVND,$TotalVND,$DisPercent,$VATPercent);
		//Thêm một sản phẩm vào trong bảng tài sản asset tbl
		function insertAsset($AssCode,$AssName,$CATCode,$QRCode,$SerialNumber,$AssDescription,$IOCode,$STAID,$LocaID,$Quantity,$Unit,$ModifiedBy,$Warranty,$Price,$Amount);
		function updateAsset($AssID,$AssCode,$AssName,$CATCode,$QRCode,$SerialNumber,$AssDescription,$STAID,$LocaID,$Quantity,$Unit,$ModifiedBy,$Warranty,$Price,$Amount);
		
		function getNumAssetByCat($CatCode,$Year);
		function addZeroDigit($number,$numDigit);
		function getAssetListOfInvcByIOCode($IOCode);
		function getInvcInfoByIOCode($IOCode);
		function getInvoiceList($InvcNo,$SuppID,$SuppName,$FromDate,$ToDate,$Description);
		function insertIOList($type,$costCode,$dateCreate,$delivery,$receipt,$roomExp,$roomImp,$note,$modifiedBy);
		
		//Cập nhật thông tin phiếu nhập xuất
		function updateIOList($costCode,$dateCreate,$delivery,$receipt,$payCash,$note,$modifiedBy);
		function getBarcodeListByIOCode($IOCode);
		function getSupplierByIOCode($IOCode);
		function deleteRecvNote($IOCode,$ModifiedBy);
		function deleteAssetByRecvNote($IOCode,$ModifiedBy);
		
		//Lấy thông tin của phiếu xuất theo mã phiếu xuất
		function getIOInfo_IOCode($IOCode);		
		function inventoryAsset($invcIDArr,$RoomArr,$AssetCodeArr);
		function staticAsset($invcIDArr,$RoomArr,$AssetCodeArr,$CatCode,$FromDate,$ToDate);
		function getIOCodeByInvcText($invcText);
		function getBarcodeListByRoomID($RoomID);
		function AssetNoBarcode($invcIDArr,$RoomArr,$AssetCodeArr,$CatCode,$FromDate,$ToDate,$assetName);
		function getQuantity_NoBarCode($FromDate,$ToDate);
		function getNhap_NoBarCode($FromDate,$ToDate);
	}
	
	class io_class implements io_class_info
	{
		function io_class()
		{
			@session_start(); 
			require_once('../../lib/'.'BaseDir.inc.htm');
			require_once(EIU_LIB.'DBCommon.class.htm');					
		}		
					
		function getInvcList($InvoiceID,$RecvNoteCode,$FromDate,$ToDate,$ReceiptPerson,$DeliveryPerson,$RecvRoom,$Note){

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT InvcNo,InvoiceID,RecvNoteCode,ReceiptDate,ReceiptPerson,DeliveryPerson,Remark,DiscountVND,SubTotalVND,VATAmountVND,TotalVND,DisPercent,VATPercent
				FROM (
						SELECT InvoiceID,RecvNoteCode,ReceiptDate,ReceiptPerson,DeliveryPerson,Remark,DiscountVND,SubTotalVND,VATAmountVND,TotalVND,DisPercent,VATPercent
						FROM recv_note_tbl 
						WHERE 
							InvoiceID LIKE '%".$InvoiceID."%' AND 
							RecvNoteCode LIKE '%".$RecvNoteCode."%' AND 
							Remark LIKE '%".$Note."%' AND 
							DeliveryPerson LIKE '%".$DeliveryPerson."%' AND
							ReceiptPerson LIKE '%".$ReceiptPerson."%' AND
							ReceiptDate BETWEEN '".$FromDate."' AND '".$ToDate."' AND
							isDeleted=0 
					) AS Recv INNER JOIN `invoice_tbl` ON InvoiceID = InvcID
				ORDER BY `RecvNoteCode` DESC 
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;			
			
		}
		
		function getIOList($CostCode,$FromDate,$ToDate,$ReceiptPerson,$DeliveryPerson,$Remark){

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT CostCode,DeliveryReceiptDate,ReceiptPerson,DeliveryPerson,Note 
				FROM io_tbl 
				WHERE 
					CostCode LIKE '%".$CostCode."%' AND 
					ReceiptPerson LIKE '%".$ReceiptPerson."%' AND 
					DeliveryPerson LIKE '%".$DeliveryPerson."%' AND 
					Note LIKE '%".$Remark."%' AND
					DeliveryReceiptDate BETWEEN '".$FromDate."' AND '".$ToDate."' AND
					isDeleted=0 ORDER BY `CostCode` ASC
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;			
			
		}

		function insertIORow($invcID,$IOCode,$ReceiptDate,$revPerson,$devPerson,$note,$modifiedBy,$DiscountVND,$SubTotalVND,$VATAmountVND,$TotalVND,$DisPercent,$VATPercent)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				INSERT INTO `recv_note_tbl` 
					(`ID`, `InvoiceID`, `RecvNoteCode`, `ReceiptDate`, 
					`ReceiptPerson`, `DeliveryPerson`, `Remark`, 
					`isDeleted`, `CreateDate`,  `ModifiedBy`,
					`DiscountVND`,`SubTotalVND`,`VATAmountVND`,
					`TotalVND`,`DisPercent`,`VATPercent`
					) 
				VALUES (NULL, '".$invcID."', '".$IOCode."', '".$ReceiptDate."', 
				'".$revPerson."', '".$devPerson."', '".$note."', 
				'0',CURRENT_TIMESTAMP, '".$modifiedBy."',
				'".$DiscountVND."','".$SubTotalVND."','".$VATAmountVND."',
				'".$TotalVND."','".$DisPercent."','".$VATPercent."'
				);				
			";			
			
			//echo $q;
			$rs_insert = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_insert)	return $rs_insert;
			else return false;	
			
		}
		
		function updateIORow($revNoteID,$invcID,$ReceiptDate,$revPerson,$devPerson,$note,$modifiedBy,$DiscountVND,$SubTotalVND,$VATAmountVND,$TotalVND,$DisPercent,$VATPercent)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				UPDATE `recv_note_tbl` 
				SET `ModifiedBy` = '".$modifiedBy."', `InvoiceID` = '".$invcID."', `ReceiptDate` = '".$ReceiptDate."', `ReceiptPerson` = '".$revPerson."',
				DeliveryPerson = '".$devPerson."', DiscountVND = '".$DiscountVND."', SubTotalVND = '".$SubTotalVND."',
				VATAmountVND = '".$VATAmountVND."',TotalVND = '".$TotalVND."',DisPercent = '".$DisPercent."',
				VATPercent = '".$VATPercent."', LastUpdate =  CURRENT_TIMESTAMP
				WHERE `recv_note_tbl`.`ID` = '".$revNoteID."';
			";
			
			//echo $q;
			$rs_update = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			return 	$rs_update;
		}
		
		function insertIOList($type,$costCode,$dateCreate,$delivery,$receipt,$roomExp,$roomImp,$note,$modifiedBy)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q ="INSERT INTO `io_tbl`(`Type`, `CostCode`, `DeliveryReceiptDate`,`ReceiptPerson`,`DeliveryPerson`, `RoomID_Out`, `RoomID_Input`, `Note`,`isDeleted`, `CreateDate`, `LastUpdate`, `ModifiedBy`) 
				VALUES (".$type.", '".$costCode."', '".$dateCreate."', '".$delivery."','".$receipt."', '".$roomImp."', '".$roomExp."', '".$note."', '0',CURRENT_TIMESTAMP,'', '".$modifiedBy."')";			
			
			//echo $q;
			$rs_insert = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_insert)	return $rs_insert;
			else return false;	
		}
		function updateIOList($costCode,$dateCreate,$delivery,$receipt,$payCash,$note,$modifiedBy)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q ="Update `io_tbl` set `DeliveryReceiptDate`='".$dateCreate."',`ReceiptPerson`='".$receipt."',`DeliveryPerson`='".$delivery."',`Note`='".$note."',`ModifiedBy`='".$modifiedBy."',`PayCash` = '".$payCash."' where `CostCode`='".$costCode."'";			
			
			//echo $q;
			$rs_update = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_update)	return $rs_update;
			else return false;	
		}
		function insertAsset($AssCode,$AssName,$CATCode,$QRCode,$SerialNumber,$AssDescription,$IOCode,$STAID,$LocaID,$Quantity,$Unit,$ModifiedBy,$Warranty,$Price,$Amount)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				INSERT INTO `asset_tbl` 
					(`AssID` ,`AssCode`,`AssName`, `CATCode`, `QRCode`, 
					`SerialNumber`, `AssDescription`, `IOCode`, `STAID`, 
					`LocaID`, `Quantity`, `Unit`, `isDeleted`, 
					`CreateDate`, `ModifiedBy`,`Warranty`,`Price`,`Amount`) 
				VALUES 
					(NULL, '".$AssCode."','".$AssName."', '".$CATCode."', '".$QRCode."', 
					'".$SerialNumber."', '".$AssDescription."', '".$IOCode."', '".$STAID."', 
					'".$LocaID."', '".$Quantity."', '".$Unit."', '0', 
					CURRENT_TIMESTAMP, '".$ModifiedBy."','".$Warranty."','".$Price."','".$Amount."');				
			";			
			
			//echo $q;
			$rs_insert = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			return $rs_insert;			
		}
		
		function updateAsset($AssID,$AssCode,$AssName,$CATCode,$QRCode,$SerialNumber,$AssDescription,$STAID,$LocaID,$Quantity,$Unit,$ModifiedBy,$Warranty,$Price,$Amount)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				UPDATE `asset_tbl` 
				SET 
					`AssCode` = '".$AssCode."',`AssName` = '".$AssName."',`CATCode` = '".$CATCode."',
					`QRCode` = '".$QRCode."',`SerialNumber` = '".$SerialNumber."',`AssDescription` = '".$AssDescription."',
					`STAID` = '".$STAID."',`LocaID` = '".$LocaID."',`Quantity` = '".$Quantity."',
					`Unit` = '".$Unit."',`ModifiedBy` = '".$ModifiedBy."',`Warranty` = '".$Warranty."',
					`Price` = '".$Price."',`Amount` = '".$Amount."',`Unit` = '".$Unit."',
					`Amount` = '".$Amount."', `LastUpdate` = CURRENT_TIMESTAMP
				WHERE `AssID` = '".$AssID."'
			";
			
			/*
			$q = "
				INSERT INTO `asset_tbl` 
					(`AssID` ,`AssCode`,`AssName`, `CATCode`, `QRCode`, 
					`SerialNumber`, `AssDescription`, `IOCode`, `STAID`, 
					`LocaID`, `Quantity`, `Unit`, `isDeleted`, 
					`CreateDate`, `ModifiedBy`,`Warranty`,`Price`,`Amount`) 
				VALUES 
					(NULL, '".$AssCode."','".$AssName."', '".$CATCode."', '".$QRCode."', 
					'".$SerialNumber."', '".$AssDescription."', '".$IOCode."', '".$STAID."', 
					'".$LocaID."', '".$Quantity."', '".$Unit."', '0', 
					CURRENT_TIMESTAMP, '".$ModifiedBy."','".$Warranty."','".$Price."','".$Amount."');				
			";			
			*/
			//echo $q;
			$rs_insert = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			return $rs_insert;			
		}
		
		function deleteRecvNote($IOCode,$ModifiedBy)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				UPDATE `recv_note_tbl`
				SET isDeleted = 1, ModifiedBy = '".$ModifiedBy."'
				WHERE RecvNoteCode LIKE '".$IOCode."'								
			";			
			
			//echo $q;
			$rs_del = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_del)	return $rs_del;
			else return false;			
		}
		
		function deleteAssetByRecvNote($IOCode,$ModifiedBy)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q = "
				UPDATE `asset_tbl`
				SET isDeleted = 1, ModifiedBy = '".$ModifiedBy."'
				WHERE IOCode LIKE '".$IOCode."'								
			";			
			
			//echo $q;
			$rs_del = $oDB->stl_db_change($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_del)	return $rs_del;
			else return false;		
		}
		
		function getNumAssetByCat($CatCode,$Year)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			//$q="Select count(*) as NumFinish From asset_tbl WHERE AssCode LIKE '%".$CatCode.$Year."%'";						
			$q = "
				SELECT MAX(CONVERT((RIGHT(AssCode,5)), SIGNED INTEGER)) AS NumFinish
				FROM asset_tbl 
				WHERE AssCode LIKE '".$CatCode.$Year."%' 				
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs[0]->{"NumFinish"};
			else return 0;	
		}
		
		function addZeroDigit($number,$numDigit)
		{			
			//$number = 1;
			//$numDigit = 5;		

			$newNumber = $number;
			$numDiditOfNumber = 0;
			$times = 0;
			
			do{
				$number = floor($number/10);
				$numDiditOfNumber++;
			}while($number);
			
			$numOfZero = $numDigit - $numDiditOfNumber;
			
			do{		
				$newNumber = "0".$newNumber;
				$times++;
			}while($times<$numOfZero);
			
			return $newNumber;
		}
		
		function getNumIOByType($type,$year,$month)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT MAX(CONVERT((RIGHT(RecvNoteCode,3)), SIGNED INTEGER)) AS NumFinish
				FROM recv_note_tbl 
				WHERE 
					Type = '".$type."' AND RecvNoteCode LIKE '_".$year.$month."%'					
				
			";
			
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs[0]->{"NumFinish"};
			else return 0;	
		}
		
		function getAssetListOfInvcByIOCode($IOCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT AssID,AssName,CATCode,CONCAT(`CATCode`,`AssName`) AS GroupByInfo,STAID,SUM(Quantity) AS Quantity,Unit,AssDescription,Warranty,Price,Amount
				FROM asset_tbl 
				WHERE IOCode LIKE '".$IOCode."' AND isDeleted = 0
				GROUP BY GroupByInfo
				ORDER BY AssID ASC
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		//lay thong tin phieu nhap theo IOcode
		function getIOInfo_IOCode($IOCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			//$q = "SELECT i.`DeliveryReceiptDate`,i.`ReceiptPerson`,i.`DeliveryPerson`,i.`RoomID_Out`,i.`RoomID_Input`,i.`Note`, r.`RoomName` as romExp,b.`BlockName` as BlockExp,r1.`RoomName` as romImp,b1.`BlockName` as BlockImp FROM `io_tbl` as i INNER JOIN `room_tbl` as r ON (i.`RoomID_Out`=r.`RoomID`) INNER JOIN `block_tbl` as b ON (r.`BlockID`=b.`BlockID`) INNER JOIN  `room_tbl` as r1 ON (i.`RoomID_Input`=r1.`RoomID`) INNER JOIN  `block_tbl` as b1 ON (r1.`BlockID`=b1.`BlockID`)  WHERE i.`isDeleted`=0 and i.`CostCode`='".$IOCode."'";
			$q = "
				SELECT 
					`ID`,`DeliveryReceiptDate`,`ReceiptPerson`,
					`DeliveryPerson`,`DiscountVND`,`DisPercent`,
					`SubTotalVND`,`VATAmountVND`,`TotalVND`,
					`VATPercent`,`Note`,`ReceiptPersonID`,`DeliveryPerson`,`PayCash`
				FROM `io_tbl` 
				WHERE `isDeleted` = 0	AND CostCode = '".$IOCode."'
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;
		}
		function getInvcInfoByIOCode($IOCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT 
					invRepTB.`ID`,invRepTB.`InvoiceID`,invRepTB.`Type`,invRepTB.`ReceiptDate`,invRepTB.`ReceiptPerson`,
					invRepTB.`DeliveryPerson`,invRepTB.`Remark` ,invoice_tbl.`InvcNo`,invRepTB.`DiscountVND`,invRepTB.`SubTotalVND`,invRepTB.`VATAmountVND`,invRepTB.`TotalVND`,invRepTB.`DisPercent`,invRepTB.`VATPercent`
				FROM (
				SELECT 
					`ID`,`InvoiceID`,`Type`,`ReceiptDate`,`ReceiptPerson`,
					`DeliveryPerson`,`Remark`,`DiscountVND`,`SubTotalVND`,`VATAmountVND`,`TotalVND`,`DisPercent`,`VATPercent`
				FROM `recv_note_tbl` 
				WHERE `isDeleted` = 0 AND `RecvNoteCode` = '".$IOCode."'
				) AS invRepTB LEFT JOIN `invoice_tbl` ON invRepTB.InvoiceID = invoice_tbl.InvcID 
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		function getInvoiceList($InvcNo,$SuppID,$SuppName,$FromDate,$ToDate,$Description){

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT InvcID,InvcNo,SuppID,SuppName,InvcDate,Description
				FROM invoice_tbl 
				WHERE 
					InvcNo LIKE '%".$InvcNo."%' AND 
					SuppID LIKE '%".$SuppID."%' AND 
					SuppName LIKE '%".$SuppName."%' AND 
					Description LIKE '%".$Remark."%' AND
					InvcDate BETWEEN '".$FromDate."' AND '".$ToDate."' AND
					isDeleted=0 ORDER BY `InvcNo` ASC
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;			
			
		}
		
		function getBarcodeListByIOCode($IOCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT AssCode,AssName,CATCode,AssDescription
				FROM asset_tbl 
				WHERE 
					IOCode LIKE '".$IOCode."' AND AssCode != '' AND				
					isDeleted=0 ORDER BY `AssID` ASC
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;
		}
		
		function getBarcodeListByRoomID($RoomID)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT Ass.AssCode,Ass.AssName,Ass.CATCode,Ass.AssDescription,Ass.IOCode,invoice_tbl.InvcNo AS InvcNo,invoice_tbl.SuppName AS SuppName
				FROM (
					SELECT AssCode,AssName,CATCode,AssDescription,IOCode 
					FROM asset_tbl  
					WHERE  
						LocaID = '".$RoomID."' AND AssCode != '' AND 				
						isDeleted=0 ORDER BY `AssID` ASC
					) AS Ass INNER JOIN recv_note_tbl ON Ass.IOCode = recv_note_tbl.RecvNoteCode
							INNER JOIN invoice_tbl ON recv_note_tbl.InvoiceID = invoice_tbl.InvcID
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;
		}
		
		function getSupplierByIOCode($IOCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT InvcNo,SuppName,InvcDate AS NgayKyHD,ReceiptDate AS NgayNhapTB
				FROM recv_note_tbl LEFT JOIN invoice_tbl ON recv_note_tbl.InvoiceID = invoice_tbl.InvcID
				WHERE 
					RecvNoteCode LIKE '".$IOCode."' AND 					
					recv_note_tbl.isDeleted=0 GROUP BY invoice_tbl.InvcNo
			";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs[0];
			else return false;
		}		
		
		function inventoryAsset($invcIDArr,$RoomArr,$AssetCodeArr)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$condition = " WHERE isDeleted = 0";
			
			if($CatCode != "")	$condition .= " AND CATCode = '".$CatCode."'";
			else $condition .= " AND AssCode != ''";
			
			if($invcIDArr != "") $condition .= " AND IOCode IN ('".$invcIDArr."')";
			
			if($RoomArr != "") $condition .= " AND LocaID IN ('".$RoomArr."')";
			
			if($AssetCodeArr != "") $condition .= " AND AssCode NOT IN ('".$AssetCodeArr."')";
			
			$query = "			
				SELECT AssCode,AssName,CATName,BlockName,RoomName,invoice_tbl.InvcNo,invoice_tbl.SuppName, 'Thiếu' AS Note,(PERIOD_DIFF(DATE_FORMAT(recv_note_tbl.ReceiptDate,'%y%m'),DATE_FORMAT( CURDATE(),'%y%m')) + Warranty)AS Warranty
				FROM((
						SELECT AssCode,AssName,CATCode,LocaID,IOCode,Warranty
						FROM asset_tbl
						".$condition."
					) AS AssetTBL 
								INNER JOIN room_tbl ON AssetTBL.LocaID = room_tbl.RoomID) 
								INNER JOIN block_tbl ON room_tbl.BlockID = block_tbl.BlockID
								INNER JOIN category_tbl ON category_tbl.CATCode = AssetTBL.CATCode
								INNER JOIN recv_note_tbl ON AssetTBL.IOCode = recv_note_tbl.RecvNoteCode
								INNER JOIN invoice_tbl ON recv_note_tbl.InvoiceID = invoice_tbl.InvcID						
			";
			
			//echo $query;
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
		}
		
		function getIOCodeByInvcText($invcText)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$query = "
				SELECT `RecvNoteCode`
				FROM `recv_note_tbl`
				WHERE `InvoiceID` IN (
						SELECT `InvcID`
						FROM `invoice_tbl`
						WHERE 
							`InvcNo` LIKE '%".$invcText."%' AND 					
							`isDeleted`=0 ORDER BY `InvcNo` ASC
					)
			";			
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
			
		}
		
		function staticAsset($invcIDArr,$RoomArr,$AssetCodeArr,$CatCode,$FromDate,$ToDate)
		{
		
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			
			$condition = " WHERE isDeleted = 0";
			
			if($CatCode != "")	$condition .= " AND CATCode = '".$CatCode."'";
			else $condition .= " AND AssCode != ''";
			
			if($invcIDArr != "") $condition .= " AND IOCode IN ('".$invcIDArr."')";
			
			if($RoomArr != "") $condition .= " AND LocaID IN ('".$RoomArr."')";
			
			if($AssetCodeArr != "") $condition .= " AND AssCode IN ('".$AssetCodeArr."')";
			
			if(($FromDate!="")&&($ToDate!="")) $condition .= " And DATE(`CreateDate`) between '".$FromDate."' AND '".$ToDate."'";
			  
			
			$query = "			
				SELECT AssCode,AssName,CATName,BlockName,RoomName,invoice_tbl.InvcNo,invoice_tbl.SuppName, '' AS Note,recv_note_tbl.ReceiptDate,(PERIOD_DIFF(DATE_FORMAT(recv_note_tbl.ReceiptDate,'%y%m'),DATE_FORMAT( CURDATE(),'%y%m')) + Warranty)AS Warranty
				FROM((
						SELECT AssCode,AssName,CATCode,LocaID,IOCode,Warranty
						FROM asset_tbl
						".$condition."
					) AS AssetTBL 
								INNER JOIN room_tbl ON AssetTBL.LocaID = room_tbl.RoomID) 
								INNER JOIN block_tbl ON room_tbl.BlockID = block_tbl.BlockID
								INNER JOIN category_tbl ON category_tbl.CATCode = AssetTBL.CATCode
								INNER JOIN recv_note_tbl ON AssetTBL.IOCode = recv_note_tbl.RecvNoteCode
								INNER JOIN invoice_tbl ON recv_note_tbl.InvoiceID = invoice_tbl.InvcID			
			
			";
			
			//echo $query;
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
			
		}
		function AssetNoBarcode($invcIDArr,$RoomArr,$AssetCodeArr,$CatCode,$FromDate,$ToDate,$assetName)
		{
		
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			
			$condition = " WHERE isDeleted = 0";
			
			if($CatCode != "")	$condition .= " AND CATCode = '".$CatCode."'";
			
			if($assetName!="")  $condition.="AND AssName='".$assetName."'";
			
			//if($invcIDArr != "") $condition .= " AND IOCode IN ('".$invcIDArr."')";
			
			if($RoomArr != "") $condition .= " AND LocaID IN ('".$RoomArr."')";
			
			if($AssetCodeArr != "") $condition .= " AND AssCode IN ('".$AssetCodeArr."')";
			if(($FromDate!="")&&($ToDate==""))$condition .= " And DATE(CreateDate) between '".$FromDate."' AND now()";
			else if(($FromDate!="")&&($ToDate!="")) $condition .= " And DATE(CreateDate) between '".$FromDate."' AND '".$ToDate."'";
			  
			
			$query = "			
				SELECT AssName,AssetTBL.CATCode as CAT,CATName,BlockName,RoomName, SLN,Unit,AssDescription
				FROM((
						SELECT AssName,CATCode,LocaID,IOCode, sum(`Quantity`) AS SLN,Unit,AssDescription
						FROM asset_tbl
						".$condition." Group by AssName
					) AS AssetTBL 
								INNER JOIN room_tbl ON AssetTBL.LocaID = room_tbl.RoomID) 
								INNER JOIN block_tbl ON room_tbl.BlockID = block_tbl.BlockID
								INNER JOIN category_tbl ON category_tbl.CATCode = AssetTBL.CATCode and category_tbl.`hasBarQRCode`=0
								INNER JOIN recv_note_tbl ON AssetTBL.IOCode = recv_note_tbl.RecvNoteCode";
			
			//echo $query;
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
			
		}
		// lay so luong xuat cho phong ban
		function getQuantity_NoBarCode($FromDate,$ToDate)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$condition = " WHERE d.isDeleted = 0 and LEFT(IOCode,1)='X' and d.IOCode=i.CostCode";
			
			if(($FromDate!="")&&($ToDate!="")) $condition .= " And DATE(i.DeliveryReceiptDate) between '".$FromDate."' AND '".$ToDate."'";
			
			$query="SELECT d.AssName,d.CATCode,sum(d.Quantifty) as SLX 
			        FROM detail_io_tbl as d, io_tbl as i".$condition."
					Group By d.AssName";
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
		}
		// lay so luong nhap ve tu phong ban
		function getNhap_NoBarCode($FromDate,$ToDate)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$condition = " WHERE d.isDeleted = 0 and LEFT(IOCode,1)='N' and d.IOCode=i.CostCode";
			
			if(($FromDate!="")&&($ToDate!="")) $condition .= " And DATE(i.DeliveryReceiptDate) between '".$FromDate."' AND '".$ToDate."'";
			
			
			$query="SELECT d.AssName,d.CATCode,sum(d.Quantifty) as SLN 
			        FROM detail_io_tbl as d, io_tbl as i".$condition."
					Group By d.AssName";
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
		}
	}
?>